package com.example.treehole;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;

import com.example.treehole.base.BaseBindingActivity;
import com.example.treehole.base.BindAdapter;
import com.example.treehole.bean.Post;
import com.example.treehole.databinding.ActivityMgPostBinding;
import com.example.treehole.databinding.ItemPostBinding;
import com.example.treehole.util.Api;
import com.example.treehole.util.HttpUtil;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyPostActivity extends BaseBindingActivity<ActivityMgPostBinding> {
    private BindAdapter<ItemPostBinding, Post> adapter = new BindAdapter<ItemPostBinding, Post>() {
        @Override
        public ItemPostBinding createHolder(ViewGroup parent) {
            return ItemPostBinding.inflate(getLayoutInflater(), parent, false);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void bind(ItemPostBinding itemPostBinding, Post post, int position) {
            itemPostBinding.getRoot().setOnClickListener(view -> {
                new AlertDialog.Builder(MyPostActivity.this)
                        .setTitle("请选择")
                        .setItems(new CharSequence[]{"查看详情", "重新编辑", "删除"}, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (which == 0) {
                                    startActivity(PostDetailActivity.class, intent -> {
                                        intent.putExtra("post", post);
                                    });
                                } else if (which == 1) {
                                    startActivity(SendPostActivity.class, intent -> {
                                        intent.putExtra("post", post);
                                    });
                                } else {
                                    doDelete(post);
                                }
                            }
                        }).show();
            });
           loadImage(post.face,itemPostBinding.ivFace);
            itemPostBinding.tvUpTime.setText(post.time);
            itemPostBinding.tvUpName.setText(post.nickname);
            itemPostBinding.title.setText(post.title);
            if (!post.file.isEmpty()) {
                loadImage(post.file,itemPostBinding.ivImage);
            } else {
                itemPostBinding.ivImage.setVisibility(View.GONE);
            }
        }
    };

    private void doDelete(Post post) {
        showLoading();
        HashMap<String, Object> params = new HashMap<>();
        params.put("id", post.id);
        HttpUtil.delete(Api.DELETE_POST, params, new HttpUtil.OnNetStringListener() {
            @Override
            public void success(String data, String message) {
                adapter.getData().remove(post);
                adapter.notifyDataSetChanged();
                display$Toast(message);
            }

            @Override
            public void error(String message) {
                display$Toast(message);
            }
        });
    }


    @Override
    protected void initData() {
        viewBinder.rvTips.setAdapter(adapter);
    }


    @Override
    protected void initListener() {
    }



    private void getPostData() {
        showLoading();
        HashMap<String, Object> params = new HashMap<>();
        params.put("userid", App.user.id);
        HttpUtil.getList(Api.GET_MY_POST, params, Post.class, new HttpUtil.OnNetListListener<Post>() {
            @Override
            public void success(List<Post> data, String message) {
                dismissLoading();
                adapter.getData().clear();
                adapter.getData().addAll(data);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void error(String error) {
                toast(error);
                dismissLoading();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        getPostData();
    }
}
